package com.ruoyi.web.invest.asset.asset.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.invest.asset.asset.domain.Asset;
import com.ruoyi.web.invest.asset.asset.mapper.AssetMapper;
import com.ruoyi.web.invest.asset.asset.service.IAssetService;
import com.ruoyi.web.invest.stockApply.domain.StockApply;
import com.ruoyi.web.invest.stockApply.service.IStockApplyService;
import com.ruoyi.web.operate.stock.domain.Stock;
import com.ruoyi.web.operate.stock.service.IStockService;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 资产Service业务层处理
 *
 * @author feng_zi_xiang
 * @date 2021-08-27
 */
@Service
public class AssetServiceImpl implements IAssetService {
    @Autowired
    private AssetMapper assetMapper;
    @Autowired
    private IStockService stockService;
    @Autowired
    private IStockApplyService stockApplyService;

    /**
     * 查询资产
     *
     * @param id 资产主键
     * @return 资产
     */
    @Override
    public Asset selectAssetById(Long id) {
        return assetMapper.selectAssetById(id);
    }

    /**
     * 查询资产列表
     *
     * @param asset 资产
     * @return 资产
     */
    @Override
    public List<Asset> selectAssetList(Asset asset) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        asset.setHolderId(sysUser.getUserId());
        return assetMapper.selectAssetList(asset);
    }

    /**
     * 查询资产列表
     *
     * @param asset 资产
     * @return 资产
     */
    @Override
    public List<Asset> selectAssetListForRepo(Asset asset) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        asset.setHolderId(sysUser.getUserId());
        return assetMapper.selectAssetList(asset);
    }

    /**
     * 新增资产
     *
     * @param asset 资产
     * @return 结果
     */
    @Override
    public int insertAsset(Asset asset) {
        asset.setCreateTime(DateUtils.getNowDate());
        return assetMapper.insertAsset(asset);
    }

    /**
     * 修改资产
     *
     * @param asset 资产
     * @return 结果
     */
    @Override
    public int updateAsset(Asset asset) {
        asset.setUpdateTime(DateUtils.getNowDate());
        return assetMapper.updateAsset(asset);
    }

    /**
     * 卖出资产
     *
     * @return 结果
     */
    @Override
    public int sell(Long assetId, String price, int number) {
        Asset asset = assetMapper.selectAssetById(assetId);

        StockApply stockApply = new StockApply();
        stockApply.setStockId(asset.getStockId());
        stockApply.setApplicant(asset.getHolderName());
        stockApply.setApplicantId(asset.getHolderId());
        stockApply.setNumber(number);
        stockApply.setPrice(new BigDecimal(price));
        BigDecimal totalMoney = new BigDecimal(price).multiply(BigDecimal.valueOf(number));
        stockApply.setTotalMoney(totalMoney.toString());
        //卖出操作
        stockApply.setAction("1");
        //状态直接为确认
        stockApply.setStatus("1");
        stockApplyService.addStockApply(stockApply);
        StockApply stockApply1 = new StockApply();
        stockApply1.setCreateTime(DateUtils.getNowDate());
        //今天所有交易的数据
        List<StockApply> stockApplyList = stockApplyService.selectStockApplyList(stockApply1);
        BigDecimal averagePrice = BigDecimal.ZERO;
        for (StockApply stockApply2 : stockApplyList) {
            averagePrice = averagePrice.add(stockApply2.getPrice());
        }
        // 求出平均价格 保留两位小数
        averagePrice = averagePrice.divide(BigDecimal.valueOf(stockApplyList.size()), 2, BigDecimal.ROUND_HALF_UP);

        Stock stock = stockService.selectStockById(asset.getStockId());
        stock.setLatestPrice(averagePrice);    //更新价格
        if (averagePrice.compareTo(stock.getMinPrice()) == -1) {
            stock.setMinPrice(averagePrice);
        }
        if (averagePrice.compareTo(stock.getMaxPrice()) == 1) {
            stock.setMaxPrice(averagePrice);
        }
        stock.setTurnover(stock.getTurnover().add(NumberUtils.createBigDecimal(String.valueOf(totalMoney))));
        stock.setAmount(stock.getAmount() + number);
        stock.setUpdateTime(DateUtils.getNowDate());

        stockService.updateStock(stock);

        asset.setNumber(asset.getNumber() - number);
        asset.setUpdateTime(DateUtils.getNowDate());
        return assetMapper.updateAsset(asset);
    }

    /**
     * 卖出回购股票
     *
     * @return 结果
     */
    @Override
    public int sellForRepo(Long assetId, String price, int number) {
        Asset asset = assetMapper.selectAssetById(assetId);

        StockApply stockApply = new StockApply();
        stockApply.setStockId(asset.getStockId());
        stockApply.setApplicant(asset.getHolderName());
        stockApply.setApplicantId(asset.getHolderId());
        stockApply.setNumber(number);
        stockApply.setPrice(new BigDecimal(price));
        BigDecimal totalMoney = new BigDecimal(price).multiply(BigDecimal.valueOf(number));
        stockApply.setTotalMoney(totalMoney.toString());
        //卖出操作
        stockApply.setAction("1");
        //状态直接为确认
        stockApply.setStatus("1");
        stockApplyService.addStockApply(stockApply);
        StockApply stockApply1 = new StockApply();
        stockApply1.setCreateTime(DateUtils.getNowDate());
        //今天所有交易的数据
        List<StockApply> stockApplyList = stockApplyService.selectStockApplyList(stockApply1);
        BigDecimal averagePrice = BigDecimal.ZERO;
        for (StockApply stockApply2 : stockApplyList) {
            averagePrice = averagePrice.add(stockApply2.getPrice());
        }
        // 求出平均价格 保留两位小数
        averagePrice = averagePrice.divide(BigDecimal.valueOf(stockApplyList.size()), 2, BigDecimal.ROUND_HALF_UP);

        Stock stock = stockService.selectStockById(asset.getStockId());
        stock.setLatestPrice(averagePrice);    //更新价格
        if (averagePrice.compareTo(stock.getMinPrice()) == -1) {
            stock.setMinPrice(averagePrice);
        }
        if (averagePrice.compareTo(stock.getMaxPrice()) == 1) {
            stock.setMaxPrice(averagePrice);
        }
        stock.setTurnover(stock.getTurnover().add(NumberUtils.createBigDecimal(String.valueOf(totalMoney))));
        stock.setAmount(0L);
        stock.setAmountSum(stock.getAmountSum() - number);
        stock.setUpdateTime(DateUtils.getNowDate());

        stockService.updateStock(stock);

        asset.setNumber(asset.getNumber() - number);
        asset.setUpdateTime(DateUtils.getNowDate());
        return assetMapper.updateAsset(asset);
    }

    /**
     * 批量删除资产
     *
     * @param ids 需要删除的资产主键
     * @return 结果
     */
    @Override
    public int deleteAssetByIds(Long[] ids) {
        return assetMapper.deleteAssetByIds(ids);
    }

    /**
     * 删除资产信息
     *
     * @param id 资产主键
     * @return 结果
     */
    @Override
    public int deleteAssetById(Long id) {
        return assetMapper.deleteAssetById(id);
    }
}
